import { Form, message } from 'antd';
import React from 'react';
import { AxUpdateForm } from '@/components/AxForm/AxUpdateForm/index';
import { AxLabel } from '@/components/AxForm/AxFormItem/AxLabel/index';
import { AxPassword } from '@/components/AxForm/AxFormItem/AxInput/index';
import { axhttp } from '@/shared/axhttp';
import { axurl } from '@/shared/axurl';

/** 修改密码 */
const ChangePwd = props => {
  const { form, handleVisible } = props;
  const handleCreate = (fields) => {
    let checkPwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/g;
    if (fields.pwd !== fields.confirmPwd) {
      message.error('两次输入的新密码不一致')
      return Promise.reject();
    }
    if (!checkPwd.test(fields.pwd)) {
      message.error('密码格式由数字、字母组成，长度6-20位')
      return Promise.reject();
    }
    return axhttp.postForm(axurl.comm.changePassword, null, fields).then(res => {
      if (res && res.success) {
        message.success('成功');
        handleVisible(false);
      } else {
        return Promise.reject();
      }
    });
  }
  return (
    <AxUpdateForm
      {...props}
      title="修改密码"
      handleCreate={handleCreate}
      items={[
        <AxPassword form={form} label="新密码" field="pwd" required={true} minLength={6} maxLength={20} />,
        <AxPassword form={form} label="确认新密码" field="confirmPwd" required={true} minLength={6} maxLength={20} />
      ]}
    />
  );
};

export default Form.create()(ChangePwd);
